
package com.compuvest.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

public class Clickstream implements java.io.Serializable {

    private List<String> clickstream;
    private Date startRequest;
    private Date lastRequest;
    private String hostname;
    private String advertSite;
    private String ipAddress;
    private String advertName;
    private String initialReferrer;
    private MySQL sb;
    private String agent;

    public Clickstream() {
	clickstream = new ArrayList<>();
	startRequest = lastRequest = new Date();
    }

    public void addRequest(HttpServletRequest request) {

        lastRequest = new Date();

	if(hostname == null) {
	    hostname = request.getRemoteHost();
	    ipAddress = request.getRemoteAddr();

            advertName = request.getParameter("advsite");
//            System.out.println("URI=" + request.getRequestURI());

	    boolean bot = false;
	    String ua = request.getHeader("USER-AGENT") == null ? "" : request.getHeader("USER-AGENT").toLowerCase();

	    if(ua.contains("win95")) {
                agent = "Win95 Netscape3.0";
            }
	    else if(ua.contains("safari")) {
                agent = "Macintosh Safari";
            }
	    else if(ua.contains("macintosh")) {
                agent = "Macintosh Netscape3.01";
            }
	    else if(ua.contains("netscape")) {
                agent = "Netscape";
            }
	    else if(ua.contains("msie")) {
                agent = "MSIE";
            }
	    else if(ua.contains("konqueror")) {
                agent = "Konqueror";
            }
	    else if(ua.contains("opera")) {
                agent = "Opera";
            }
	    else if(ua.contains("firefox/")) {
                agent = "Firefox";
            }
	    else if(ua.contains("gecko/")) {
                agent = "Mozilla";
            }
	    else {
		bot = true;
//		System.out.println("URI=" + request.getRequestURI() + " agent=" + ua);
	    }

	    // stat bots
	    if(bot) {
		agent = "bots";
		advertSite = "Bots";
	    }
	    else {
		advertSite = request.getParameter("advsite") == null ? "compuvest" : (request.getParameter("advsite").length() > 15 ? advertSite.substring(0, 15) : request.getParameter("advsite"));
            }

	    initialReferrer = (request.getHeader("REFERER") == null) ? "" : request.getHeader("REFERER");

            sb = (MySQL)request.getSession().getServletContext().getAttribute("sb");
	    sb.saveCustomerLog(advertSite);
        }

//        clickstream.add(request.getServerName() + request.getRequestURI());

//	clickstream.add(request.getServerName() + (request.getServerPort() != 80? ":" + request.getServerPort(): "") +
//			request.getRequestURI() +
//			(request.getQueryString() != null ? "?" + request.getQueryString(): "" ));
    }

    public String getAdvertName() {
        return  advertName == null ? "compuvest" : advertName.length() > 15 ? advertName.substring(0,15) : advertName;
    }
    public String getAdvertSite()       { return advertSite; }
    public String getHostname()		{ return hostname; }
    public String getIPAddress()	{ return ipAddress == null ? "0" : ipAddress; }
    public List getStream()		{ return clickstream; }
    public String getInitialReferrer()	{ return initialReferrer; }
    public Date getStartRequest()	{ return startRequest; }
    public Date getLastRequest()	{ return lastRequest; }
    public String getAgent()		{ return agent; }

    @Override
    public String toString() {
        StringBuilder bul = new StringBuilder();

        bul.append("\nadvertName:      ").append(getAdvertName());
        bul.append("\nadvertSite:      ").append(advertSite);
        bul.append("\nhostname:        ").append(hostname);
        bul.append("\nipaddress:       ").append(ipAddress);
        bul.append("\nclickstream:     ").append(clickstream.toString());
        bul.append("\ninitialReferrer: ").append(initialReferrer);
        bul.append("\nstartRequest:    ").append(startRequest);
        bul.append("\nlastRequest:     ").append(lastRequest);
        bul.append("\nagent:           ").append(agent);

        return bul.toString();
    }

}
